home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C06 QuickDraw GX / P06 QD GX Mapping / QDGXMapping.c next >
Encoding:
C/C++ Source or Header  |  1995-09-01  |  4.4 KB  |  190 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    QDGXMapping.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #include "PrintingManager.h"      // defines the Gestalt selector code gestaltGXPrintingMgrVersion
  13. #include "graphics macintosh.h"   // defines the Gestalt selector code gestaltGraphicsVersion
  14. #include "graphics toolbox.h"     // prototype for the GX function GXNewWindowViewPort()
  15. #include "graphics libraries.h"   // prototypes for several GX functions
  16.  
  17.  
  18. //____________________________________________________________
  19.  
  20. Boolean  IsQuickDrawGXAvailable( void );
  21. void     InitializeToolbox( void );
  22. void     InitializeQuickDrawGX( void );
  23. void     OpenDisplayWindow( void );
  24. void     CleanUpQuickDrawGXandQuit( void );
  25. void     CreateGXRectangleShape( void );
  26. void     ScaleGXRectangleShape( void );
  27.  
  28.  
  29. //____________________________________________________________
  30.  
  31. #define     kGXClientHeapSizeBytes    150 * 1024
  32.  
  33.  
  34. //____________________________________________________________
  35.  
  36. gxGraphicsClient  gGXClient;
  37. Boolean           gQuickDrawGXPresent;
  38. WindowPtr         gDisplayWindow = nil;
  39. gxViewPort        gWindowViewPort;
  40. Boolean           gDone = false;
  41. gxShape           gRectShape;
  42.  
  43.  
  44. //____________________________________________________________
  45.  
  46. void  main( void )
  47. {
  48.    InitializeToolbox();
  49.  
  50.    gQuickDrawGXPresent = IsQuickDrawGXAvailable();
  51.    if ( gQuickDrawGXPresent == true )
  52.       InitializeQuickDrawGX();
  53.    else
  54.       ExitToShell();
  55.  
  56.    OpenDisplayWindow();
  57.  
  58.    while ( gDone == false )
  59.    {
  60.       if ( Button() )
  61.          CleanUpQuickDrawGXandQuit();
  62.    }      
  63. }
  64.  
  65.  
  66. //____________________________________________________________
  67.  
  68. Boolean  IsQuickDrawGXAvailable( void )
  69. {
  70.    OSErr  theError;
  71.    long   theResult;
  72.    
  73.    theError = Gestalt( gestaltGraphicsVersion, &theResult );
  74.    if ( theError != noErr )
  75.       return ( false );
  76.  
  77.    theError = Gestalt( gestaltGXPrintingMgrVersion, &theResult );
  78.    if ( theError != noErr )
  79.       return ( false );
  80.       
  81.    return ( true );
  82. }
  83.  
  84.  
  85. //____________________________________________________________
  86.  
  87. void  InitializeQuickDrawGX( void )
  88. {
  89.    gxGraphicsError  theGXgraphicsError;
  90.    OSErr            theGXprintError;      
  91.    
  92.    gGXClient = GXNewGraphicsClient( nil, kGXClientHeapSizeBytes, 0L );
  93.  
  94.    GXEnterGraphics();
  95.    theGXgraphicsError = GXGetGraphicsError( nil );
  96.    if ( theGXgraphicsError == out_of_memory )
  97.       ExitToShell();
  98.  
  99.    theGXprintError = GXInitPrinting();
  100.    if ( theGXprintError != noErr )
  101.       ExitToShell();
  102. }
  103.  
  104.  
  105. //____________________________________________________________
  106.  
  107. void  OpenDisplayWindow( void )
  108. {
  109.    gDisplayWindow = GetNewCWindow( 128, nil, (WindowPtr)-1L ); 
  110.    ShowWindow( gDisplayWindow );
  111.    SetPort( gDisplayWindow );
  112.    
  113.    gWindowViewPort = GXNewWindowViewPort( gDisplayWindow );
  114.  
  115.    CreateGXRectangleShape();
  116.  
  117.    GXSetShapeViewPorts( gRectShape, 1, &gWindowViewPort );
  118.  
  119.    GXDrawShape( gRectShape );
  120.  
  121.    ScaleGXRectangleShape();
  122.  
  123.    GXDrawShape( gRectShape );
  124.  
  125. }
  126.  
  127.  
  128. //____________________________________________________________
  129.  
  130. void  CreateGXRectangleShape( void )
  131. {
  132.    gxRectangle  theRectGeometry = { ff(200), ff(40), ff(320), ff(120) };                           
  133.  
  134.    gRectShape = GXNewShape( gxRectangleType );
  135.    GXSetRectangle( gRectShape, &theRectGeometry );
  136. }
  137.  
  138.  
  139. //____________________________________________________________
  140.  
  141. void  ScaleGXRectangleShape( void )
  142. {
  143.    Fixed  theHorizScale;
  144.    Fixed  theVertScale;
  145.    Fixed  theXOffset = ff(0);
  146.    Fixed  theYOffset = ff(0);
  147.  
  148.    theHorizScale = FixedDivide( ff(1), ff(2) );
  149.    theVertScale  = FixedDivide( ff(1), ff(2) );
  150.  
  151.    GXScaleShape( gRectShape, theHorizScale, theVertScale, theXOffset, theYOffset );
  152. }
  153.  
  154.  
  155. //____________________________________________________________
  156.  
  157. void  CleanUpQuickDrawGXandQuit( void )
  158. {
  159.    OSErr  theGXprintError;      
  160.  
  161.    if ( gDisplayWindow != nil )
  162.    {
  163.       GXDisposeViewPort( gWindowViewPort );
  164.       DisposeWindow( gDisplayWindow );  
  165.    }
  166.    
  167.    theGXprintError = GXExitPrinting();
  168.  
  169.    GXExitGraphics();
  170.    
  171.    GXDisposeGraphicsClient( gGXClient ); 
  172.  
  173.    gDone = true;
  174. }
  175.  
  176.  
  177. //____________________________________________________________
  178.  
  179. void  InitializeToolbox( void )
  180. {
  181.    InitGraf( &qd.thePort );
  182.    InitFonts();
  183.    InitWindows();
  184.    InitMenus();
  185.    TEInit();
  186.    InitDialogs( 0L );
  187.    FlushEvents( everyEvent, 0 );
  188.    InitCursor();
  189. }
  190.